package com.example.demo.exception;

import com.example.demo.config.RespBean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice("com.example.demo")
public class ExceptionAdvice {

    /**
     * 通用异常处理方法
     * 指定拦截exception的类型，不写会默认拦截传参的异常
     */
    @ExceptionHandler(Exception.class)
    public Object exceptionHandler(Exception e){
        System.out.println(e.getMessage());
        return RespBean.error("服务器异常！",e.getMessage());
    }

    @ExceptionHandler(CommonException.class)
    public Object exceptionHandler(CommonException e){
        System.out.println(e.getMsg());
        return RespBean.error("自定义异常！",e.getMsg());
    }

}
